import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import static java.lang.System.exit;

public class Game {
	IOHelper helper = new IOHelper();
	Chessboard board=new Chessboard();
	Result result=Result.DRAW;

	// add some attribute here

	// Display the chessboard
	public void initGame() {
		// add some code here
		String cling="Welcom to TicTacToe.\nThe first user'chess will be O and the second user's will be X\n";
		helper.writeToConsole(cling);
		helper.writeToConsole(board.toString());

	}

	// Play each round of the game until game over
	public void startGame() {
		// add some code here
		while(true){
			Chess chess1=new Chess(Player.O_PLAYER, helper.readFromConsole());
			board.addOneChess(chess1);
			result=board.judge();
			helper.writeToConsole(board.toString());
			this.gameOver(result);
			Chess chess2=new Chess(Player.X_PLAYER,helper.readFromConsole());
			board.addOneChess(chess2);
			helper.writeToConsole(board.toString());
			result=board.judge();
			this.gameOver(result);
		}
		
	}

	// Show the result of the game, and then exit the game.
	public void gameOver(Result result) {
		// add some code here
		if (result != Result.GAMING){
			helper.writeToConsole("Game Over!! ");
			helper.writeToConsole(result.toString());
			
			exit(0);
		}
			
	}

	public static void main(String[] args) {
		Game game = new Game();
		game.initGame();
		game.startGame();
	}
}
